package com.malou.client.util;

import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.CurrencyData;
import com.google.gwt.i18n.client.CurrencyList;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.i18n.client.constants.CurrencyCodeMapConstants;

public class MyNumberFormat extends NumberFormat{

    private static CurrencyCodeMapConstants currencyCodeMapConstants = GWT.create(CurrencyCodeMapConstants.class);

    protected MyNumberFormat(String pattern, CurrencyData cdata,
            boolean userSuppliedPattern) {
        super(pattern, cdata, userSuppliedPattern);     
    }

    public static NumberFormat getCurrencyFormat(String currencyCode) {     
        return new MyNumberFormat(defaultNumberConstants.currencyPattern(),
            lookupCurrency(currencyCode), false);
      }

      private static CurrencyData lookupCurrency(String currencyCode) {
        CurrencyData currencyData = CurrencyList.get().lookup(currencyCode);

        @SuppressWarnings("rawtypes")
		Map currencyMap = currencyCodeMapConstants.currencyMap();       

        String code = currencyData.getCurrencyCode();
        //String symbol = currencyData.getCurrencySymbol();
        String symbol = (String) currencyMap.get(currencyCode);
        int fractionDigits = currencyData.getDefaultFractionDigits();
        String portableSymbol = currencyData.getPortableCurrencySymbol();       

        return toCurrencyData(code, symbol, fractionDigits, portableSymbol);
      }

      public static native CurrencyData toCurrencyData(String code, String symbol, int fractionDigits, String portableSymbol) /*-{
        return [ code, symbol, fractionDigits, portableSymbol ];
      }-*/;
}
